﻿using HCQ2_BLL.Order;
using HCQ2_Common;
using HCQ2_Common.Constant;
using HCQ2_IBLL;
using HCQ2_IDAL;
using HCQ2_Model;
using HCQ2_Model.Data;
using HCQ2_Model.Pay;
using HCQ2_Model.ShoppingCart;
using HCQ2_Model.ViewModel.VR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_BLL
{
    /// <summary>
    /// 购物车相关    
    /// </summary>
    public partial class T_VRShoppingCartBLL: IT_VRShoppingCartBLL
    {

        public IT_VRShoppingCartDAL ShoppingCartDAL
        {
            get {
                return DBSession.IT_VRShoppingCartDAL;
            }

        }

        /// <summary>
        /// 获取用户购物车信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public UserShoppingCart GetUserShoppingCart(int UserId)
        {
            T_User user = DBSession.IT_UserDAL.FindSingle(w => w.user_id == UserId);

            UserShoppingCart cart = DBSession.IT_VRShoppingCartDAL.GetUserShoppingCart(UserId);
            cart.userGuid = user.user_guid;
            return cart;
        }

        /// <summary>
        /// 获取用户购物车商品信息
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <param name="CommodityId">选中购物车商品ID</param>
        /// <returns></returns>
        public List<T_DocumentInfo> ListUserShoppingCartCommodity(int UserId, int[] CommodityId)
        {
            return DBSession.IT_VRShoppingCartDAL.ListUserShoppingCartCommodity(UserId,CommodityId);
        }

        /// <summary>
        /// 获取用户购物车中商品已经被购买的
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <param name="CommodityId">选中购物车中商品</param>
        /// <returns></returns>
        public List<T_VROrderDetail> ListUserPurchased(int UserId, int[] CommodityId)
        {
            return DBSession.IT_VRShoppingCartDAL.ListUserPurchased(UserId,CommodityId);
        }

        /// <summary>
        /// 获取用户购物车中商品已经被：下架、删除
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <param name="CommodityId">选中购物车商品ID</param>
        /// <returns></returns>
        public List<T_DocumentInfo> ListUserLowerShelfAndRemove(int UserId, int[] CommodityId)
        {
            return DBSession.IT_VRShoppingCartDAL.ListUserLowerShelfAndRemove(UserId,CommodityId);
        }

        /// <summary>
        /// 获取选中商品总价
        /// </summary>
        /// <param name="CommodityId">选中商品ID数组</param>
        /// <returns></returns>
        public Decimal GetSelectCommodityTotalMoney(int[] CommodityId)
        {
            return DBSession.IT_VRShoppingCartDAL.GetSelectCommodityTotalMoney(CommodityId);
        }

        /// <summary>
        /// 获取选中商品中系统管理员发布的商品总价
        /// </summary>
        /// <param name="CommodityId">选中商品ID</param>
        /// <returns></returns>
        public Decimal GetSelectSystemCommodityTotalMoney(int[] CommodityId)
        {
            string systemUserId = ConfigurationHelper.AppSetting(SystemConstant.SYSTEM_USERID_NAME);

            return DBSession.IT_VRShoppingCartDAL.GetSelectSystemCommodityTotalMoney(CommodityId,systemUserId);
        }
        /// <summary>
        ///  判断用户是否加入购物车
        /// </summary>
        /// <param name="fileId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int GetCartFile(int fileId, int userId)
        {
            return DBSession.IT_VRShoppingCartDAL.GetCartFile(fileId, userId);
        }

        /// <summary>
        /// 获取选中文件进行“商品购买”的信息展示数据
        /// </summary>
        /// <param name="selectFileIdStr">选择文件的ID字符串，多个通过逗号分隔开</param>
        /// <returns></returns>
        public List<ShoppingCartListModel> GetSelectFilePayInfo(string selectFileIdStr)
        {
            return DBSession.IT_VRShoppingCartDAL.GetSelectFilePayInfo(selectFileIdStr);
        }

        /// <summary>
        /// 获取订单支付金额
        /// </summary>
        /// <param name="paymentAmountParams">请求参数</param>
        /// <returns></returns>
        public OperationResult GetPaymentAmount(PaymentAmountParams paymentAmountParams)
        {
            List<int> fileid = new List<int>();
            foreach (var p in paymentAmountParams.CommodityIds)
            {
                fileid.Add(p.CommodityId.Value);
            }

            List<string> mess = new List<string>();

            //获取所有购买文件的期限价格
            List<T_VRGoodDisiPrice> goodDisiPrices = ShoppingCartDAL.ListFileAllPrice(fileid);

            decimal TotalPrice=0;
            foreach (var commodify in paymentAmountParams.CommodityIds)
            {
                T_VRGoodDisiPrice commodifyGoodDisi = null;
                foreach (T_VRGoodDisiPrice goodDisiPrice in goodDisiPrices)
                {
                    if (goodDisiPrice.op_month == (int)commodify.PurchasePeriod.Value
                        && goodDisiPrice.file_id == commodify.CommodityId.Value)
                    {
                        commodifyGoodDisi = goodDisiPrice;
                        break;
                    }
                }

                if (commodifyGoodDisi == null)
                {
                    T_DocumentInfo documentInfo = DBSession.IT_DocumentInfoDAL.FindSingle(w => w.file_id == commodify.CommodityId);
                    mess.Add($"未找到商品：{documentInfo.file_name} 购买期限：{commodify.PurchasePeriod}个月的价格信息，请刷新重试");
                }
                else
                {
                    TotalPrice = TotalPrice + commodifyGoodDisi.ori_price;
                }
            }

            //如果有价格不存的信息，返回错误
            if (mess.Count > 0)
            {
                return new OperationResult(false,"购买商品有不存在购买期限的价格",mess);
            }

            //由于现在没有自营所以没有优化信息
            PaymentAmountModel paymentAmountModel = new PaymentAmountModel() {
                CommodityMoney = TotalPrice,
                PaymentAmount = TotalPrice
            };

            return new OperationResult(true,"操作成功", paymentAmountModel); ;
        }

    }
}
